package com.blb.ssh.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.blb.ssh.dao.UserDao;
import com.blb.ssh.entity.User;

public class UserDaoImpl implements UserDao {
	
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	public User getUserByUserName(String username) {
		Session session = sessionFactory.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		Query<User> query = session.createQuery("from User where username = :username");
		query.setParameter("username", username);
		List<User> list = query.list();
		transaction.commit();
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
